<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员登录 - 口语练习管理后台</title>
    <link th:href="@{/admin/css/bootstrap.min.css}" rel="stylesheet">
    <link th:href="@{/admin/css/bootstrap-icons.css}" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
        }

        .login-card {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 100%;
            max-width: 400px;
        }
        
        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .login-header h1 {
            color: #333;
            font-weight: 600;
            margin-bottom: 10px;
        }
        
        .login-header p {
            color: #666;
            margin: 0;
        }
        
        .form-control {
            border-radius: 12px;
            border: 2px solid #e1e5e9;
            padding: 12px 16px;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        
        .form-control:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
        }
        
        .input-group-text {
            border-radius: 12px;
            border: 2px solid #e1e5e9;
            background: transparent;
            color: #666;
        }
        
        .btn-login {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            border-radius: 12px;
            padding: 12px;
            font-size: 16px;
            font-weight: 600;
            color: white;
            transition: all 0.3s ease;
            width: 100%;
        }
        
        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
            color: white;
        }
        
        .btn-login:disabled {
            opacity: 0.6;
            transform: none;
        }
        
        .alert {
            border-radius: 12px;
            border: none;
        }
        
        .loading {
            display: none;
        }
        
        .footer-text {
            text-align: center;
            margin-top: 30px;
            color: #666;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="login-card">
        <div class="login-header">
            <i class="bi bi-shield-lock-fill" style="font-size: 48px; color: #667eea; margin-bottom: 20px;"></i>
            <h1>管理员登录</h1>
            <p>口语练习管理后台</p>
        </div>
        
        <!-- 错误消息提示 -->
        <div id="errorAlert" class="alert alert-danger" style="display: none;">
            <i class="bi bi-exclamation-triangle me-2"></i>
            <span id="errorMessage"></span>
        </div>
        
        <form id="loginForm">
            <div class="mb-3">
                <label for="username" class="form-label">用户名</label>
                <div class="input-group">
                    <span class="input-group-text">
                        <i class="bi bi-person"></i>
                    </span>
                    <input type="text" class="form-control" id="username" name="username" required autofocus>
                </div>
            </div>
            
            <div class="mb-4">
                <label for="password" class="form-label">密码</label>
                <div class="input-group">
                    <span class="input-group-text">
                        <i class="bi bi-lock"></i>
                    </span>
                    <input type="password" class="form-control" id="password" name="password" required>
                </div>
            </div>
            
            <button type="submit" class="btn btn-login">
                <span class="normal-text">
                    <i class="bi bi-box-arrow-in-right me-2"></i>登录
                </span>
                <span class="loading">
                    <i class="bi bi-hourglass-split me-2"></i>登录中...
                </span>
            </button>
        </form>
        
        <div class="footer-text">
            © 2024 口语练习系统 管理后台
        </div>
    </div>
    
    <script th:src="@{/admin/js/bootstrap.bundle.min.js}"></script>
    <script>
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();
            
            if (!username || !password) {
                showError('请输入用户名和密码');
                return;
            }
            
            // 显示加载状态
            const submitBtn = document.querySelector('.btn-login');
            const normalText = document.querySelector('.normal-text');
            const loadingText = document.querySelector('.loading');
            
            submitBtn.disabled = true;
            normalText.style.display = 'none';
            loadingText.style.display = 'inline';
            hideError();
            
            // 发送登录请求
            const formData = new FormData();
            formData.append('username', username);
            formData.append('password', password);
            
            fetch('/api/v1/auth/admin/login', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 0) {
                    // 登录成功
                    showSuccess('登录成功，正在跳转...');
                    setTimeout(() => {
                        window.location.href = '/admin';
                    }, 1000);
                } else {
                    // 登录失败
                    showError(data.message || '登录失败，请重试');
                }
            })
            .catch(error => {
                console.error('Login error:', error);
                showError('网络错误，请检查连接后重试');
            })
            .finally(() => {
                // 恢复按钮状态
                submitBtn.disabled = false;
                normalText.style.display = 'inline';
                loadingText.style.display = 'none';
            });
        });
        
        function showError(message) {
            const errorAlert = document.getElementById('errorAlert');
            const errorMessage = document.getElementById('errorMessage');
            
            errorMessage.textContent = message;
            errorAlert.style.display = 'block';
            
            // 3秒后自动隐藏错误消息
            setTimeout(hideError, 3000);
        }
        
        function hideError() {
            document.getElementById('errorAlert').style.display = 'none';
        }
        
        function showSuccess(message) {
            const errorAlert = document.getElementById('errorAlert');
            const errorMessage = document.getElementById('errorMessage');
            
            errorAlert.className = 'alert alert-success';
            errorMessage.innerHTML = '<i class="bi bi-check-circle me-2"></i>' + message;
            errorAlert.style.display = 'block';
        }
        
        // 回车键提交
        document.addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                document.getElementById('loginForm').dispatchEvent(new Event('submit'));
            }
        });
    </script>
</body>
</html>